package hongcaosp.app.android.comment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.bean.Comment;
import hongcaosp.app.android.modle.bean.CommentReply;
import hongcaosp.app.android.modle.bean.IComment;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.user.TotalCallBack;
import xlj.lib.android.base.component.LoadingDialog;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.DensityUtils;
import xlj.lib.android.base.utils.ScreenUtils;
import xlj.lib.android.base.view.BaseBottomSheetFrag;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class CommentFragment extends BaseBottomSheetFrag implements ICommentListView,OnRecycleItemClickListener,CommentGoodListener {
    private RecyclerView recyclerView;
    private TextView emptyToast,commentsCount;
    private CommentAdapter commentAdapter;

    private IVideo video;

    private LoadMoreHandler loadMoreHandler;

    public void setVideo(IVideo video) {
        this.video = video;
    }

    private CommentPresenter commentPresenter;
    private OnCommentListener onCommentListener;

    public void setOnCommentListener(OnCommentListener onCommentListener) {
        this.onCommentListener = onCommentListener;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_comment;
    }

    @Override
    public void initView() {
        emptyToast = rootView.findViewById(R.id.empty_toast);
        recyclerView = rootView.findViewById(R.id.recycler_view);
        commentsCount = rootView.findViewById(R.id.comment_count);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        commentAdapter = new CommentAdapter(getContext(), this, this);
        recyclerView.setAdapter(commentAdapter);
        loadMoreHandler = new LoadMoreHandler(linearLayoutManager) {
            @Override
            public void loadMore() {
                commentPresenter.loadMoreComments();
            }
        };
        recyclerView.addOnScrollListener(loadMoreHandler);

        commentPresenter = new CommentPresenter(this, (int) video.getVid());

        rootView.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close(true);
            }
        });

        rootView.findViewById(R.id.click_comment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentDialog commentDialog = new CommentDialog(getContext(), new CommentDialog.CommentCallBack() {
                    @Override
                    public void onComment(String content) {
                        commentVideo(content);
                    }
                });
                commentDialog.show();
            }
        });

        commentsCount.setText(video.getCommentCount() + "条评论");
        commentPresenter.freshComments(new TotalCallBack() {
            @Override
            public void onTotalSend(int total) {
                commentsCount.setText(total + "条评论");
            }
        });
    }

    /**
     *
     * @param comment
     */
    private void commentVideo(String comment) {
        LoadingDialog.show("",getContext());
        commentPresenter.comments(comment, new PresenterListener() {
            @Override
            public void onSuccess() {
                video.setCommentsCounts(video.getCommentCount() + 1);
                LoadingDialog.dismiss();
                commentsCount.setText(video.getCommentCount() + "条评论");
                ToastManager.getInstance().showToast("发送成功");
                if (onCommentListener != null) {
                    onCommentListener.onCommentCount();
                }
            }

            @Override
            public void onFail(String message) {
                LoadingDialog.dismiss();
                ToastManager.getInstance().showToast(message);
            }
        });
    }

    private void replyComment(Comment comment,String content) {
        if (UserToken.getDefault().isCurrentUser(comment.getCommentsUser().getId())) {
            ToastManager.getInstance().showToast(" 无法评论自己的评论");
            return;
        }
        LoadingDialog.show("",getContext());
        commentPresenter.reply(content, comment, new PresenterListener() {
            @Override
            public void onSuccess() {
                video.setCommentsCounts(video.getCommentCount() + 1);
                commentsCount.setText(video.getCommentCount() + "条评论");
                LoadingDialog.dismiss();
                ToastManager.getInstance().showToast("发送成功");
                if (onCommentListener != null) {
                    onCommentListener.onCommentCount();
                }
            }

            @Override
            public void onFail(String message) {
                LoadingDialog.dismiss();
                ToastManager.getInstance().showToast(message);
            }
        });
    }

    private void replyComment(CommentReply commentReply,String content) {
        if (UserToken.getDefault().isCurrentUser(commentReply.getCommentsUser().getId())) {
            ToastManager.getInstance().showToast(" 无法评论自己的评论");
            return;
        }
        LoadingDialog.show("",getContext());
        commentPresenter.reply(content, commentReply, new PresenterListener() {
            @Override
            public void onSuccess() {
                video.setCommentsCounts(video.getCommentCount() + 1);
                commentsCount.setText(video.getCommentCount() + "条评论");
                LoadingDialog.dismiss();
                ToastManager.getInstance().showToast("发送成功");
                if (onCommentListener != null) {
                    onCommentListener.onCommentCount();
                }
            }

            @Override
            public void onFail(String message) {
                LoadingDialog.dismiss();
                ToastManager.getInstance().showToast(message);
            }
        });
    }

    @Override
    public void resetView() {
        super.resetView();
        rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getScreenHeight(getContext()) - DensityUtils.dip2px(getContext(),150)));
    }

    private void freshEmptyState() {
        if (commentAdapter.getItemCount() == 0) {
            emptyToast.setVisibility(View.VISIBLE);
        } else {
            emptyToast.setVisibility(View.GONE);
        }
    }

    @Override
    public void freshList(List<IViewType> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        commentAdapter.setData(data);
        commentAdapter.notifyDataSetChanged();
        freshEmptyState();
    }

    @Override
    public void addList(List<IViewType> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        commentAdapter.addData(data);
        commentAdapter.notifyDataSetChanged();
        freshEmptyState();
    }

    @Override
    public void moreReply(int start, int end) {
        commentAdapter.notifyDataSetChanged();
        freshEmptyState();
//        commentAdapter.notifyItemRangeInserted(start,end);
    }

    @Override
    public void notifyLoadFail(String errorMsg) {
        ToastManager.getInstance().showToast(errorMsg);
    }

    @Override
    public void onItemClick(int position) {
        final IViewType viewType = commentAdapter.getItem(position);
        if (viewType instanceof CommentRange) {
            CommentRange commentRange = (CommentRange) viewType;
            commentPresenter.loadMoreReply(commentRange.comment);
        } else if (viewType instanceof Comment) {
            CommentDialog.create(getContext(), new CommentDialog.CommentCallBack() {
                @Override
                public void onComment(String content) {
                    replyComment((Comment) viewType,content);
                }
            }).show();
        } else if (viewType instanceof CommentReply) {
            CommentDialog.create(getContext(), new CommentDialog.CommentCallBack() {
                @Override
                public void onComment(String content) {
                    replyComment((CommentReply) viewType,content);
                }
            }).show();
        }
    }

    @Override
    public void onItemLongClick(int position) {

    }

    @Override
    public void onGood(IComment comment,final GoodFreshProxy goodFreshProxy) {
        commentPresenter.good(comment, new PresenterListener() {
            @Override
            public void onSuccess() {
                goodFreshProxy.freshGoodState();
            }

            @Override
            public void onFail(String message) {
                ToastManager.getInstance().showToast(message);
            }
        });
    }

    @Override
    public void onCancelGood(IComment comment, final GoodFreshProxy goodFreshProxy) {
        commentPresenter.cancelGood(comment, new PresenterListener() {
            @Override
            public void onSuccess() {
                goodFreshProxy.freshGoodState();
            }

            @Override
            public void onFail(String message) {
                ToastManager.getInstance().showToast(message);
            }
        });
    }

    public interface OnCommentListener {
        void onCommentCount();
    }
}
